package com.joekai.view_demo.QQ_step;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.joekai.view_demo.R;
import com.joekai.view_demo.utils.PxUtils;

/**
 * 仿QQ运动步数
 * @author joekai
 */
public class QQStepView  extends View {
    private int mOuterColor = Color.RED;//总进度条颜色
    private int mInnerColor = Color.BLUE;//已走颜色
    private float mBorderWidget = 20;//圆弧宽度
    private int mTextSize = 20;//字体大小
    private String mStepText;
    private int mTextColor = Color.GREEN;//字体颜色
    private Paint mOuterPaint;//总画笔
    private Paint mInnerPaint;//已走画笔
    private Paint mTextPaint;//已走画笔
    private int mStartAngle = 135;
    private int mEndAngle = 270;
    private int mMaxStep = 2000;
    private int mStep = 1000;
    private Rect mTextBounds ;

    public QQStepView(Context context) {
        this(context,null);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // 1 分析效果
        // 2 确定自定义属性，编写attrs.xml
        // 3 在布局中使用
        // 4 在自定义View中获取自定义属性
        // 7 其他
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);
        mStepText = ta.getString(R.styleable.QQStepView_stepText);
        mOuterColor = ta.getColor(R.styleable.QQStepView_outerColor, mOuterColor);
        mInnerColor = ta.getColor(R.styleable.QQStepView_innerColor, mInnerColor);
        mBorderWidget = ta.getDimension(R.styleable.QQStepView_borderWidget, mBorderWidget);
        mTextSize = ta.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, PxUtils.sp2px(mTextSize));
        mTextColor = ta.getColor(R.styleable.QQStepView_stepTextColor, mTextColor);
        //回收
        ta.recycle();

        //创建总进度条画笔
        mOuterPaint = new Paint();
        //取消锯齿
        mOuterPaint.setAntiAlias(true);
        //设置颜色
        mOuterPaint.setColor(mOuterColor);
        //设置圆弧宽度
        mOuterPaint.setStrokeWidth(mBorderWidget);
        //设置圆弧Style
        mOuterPaint.setStyle(Paint.Style.STROKE);
        //设置起始，结束圆角
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);

        //创建已走进度条画笔
        mInnerPaint = new Paint();
        //取消锯齿
        mOuterPaint.setAntiAlias(true);
        //设置颜色
        mInnerPaint.setColor(mInnerColor);
        //设置圆弧宽度
        mInnerPaint.setStrokeWidth(mBorderWidget);
        //设置圆弧Style
        mInnerPaint.setStyle(Paint.Style.STROKE);
        //设置起始，结束圆角
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);

        //创建文字画笔
        mTextPaint = new Paint();
        //抗锯齿
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);

        mTextBounds = new Rect();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //在布局文件中，可能是wrap_content ,也可能宽高不一致

        //高度不一致，取最大值，确保是个正方形

        //获取宽高的模式
        int widgetMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获取宽高的大小
        int widgetSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //保证画布大小为正方形
        setMeasuredDimension(widgetSize > heightSize ? heightSize : widgetSize, widgetSize > heightSize ? heightSize : widgetSize);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //6.1 画外圆弧
        //问题：边缘显示不全   描边有宽度  mBorderWidget
        //获取圆点
        int center = getWidth() / 2;
        //获取半径
        int radius = (int) (center - mBorderWidget / 2);
        RectF rectF = new RectF(mBorderWidget / 2, mBorderWidget / 2, getWidth() - mBorderWidget / 2, getHeight() - mBorderWidget / 2);
        canvas.drawArc(rectF, mStartAngle, mEndAngle, false, mOuterPaint);
        //6.2 画内圆弧
        if (mMaxStep == 0) return;
        float sweepAngle = (float) mStep / mMaxStep;
        canvas.drawArc(rectF, mStartAngle, sweepAngle * mEndAngle, false, mInnerPaint);


        //6.3 画文本
        mStepText = mStep + "";
        //计算的宽度 与 字体的长度有关  与字体的大小  用画笔来测量
        //获取文本的Rect
//        mTextPaint.getTextBounds(mStepText, 0, mStepText.length(), mTextBounds);

        //获取文本宽度
        float textWidget =  mTextPaint.measureText(mStepText);

        int dx = (int) (getWidth() / 2 - textWidget / 2);

        //dy代表的是：高度的一半到baseLine的距离
        //获取文字排版信息
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        //基线
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(mStepText, dx, baseLine, mTextPaint);
    }
    // 7 其他

    public synchronized void setMaxStep(int maxStep) {
        this.mMaxStep = maxStep;
    }

    public synchronized void setStep(int step){
        this.mStep = step;
        //不断绘制
        invalidate();
    }

    public synchronized void setAnimatorStep(int step){
        //属性动画
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, step);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float step = (float) valueAnimator.getAnimatedValue();
                setStep((int) step);
            }
        });
        valueAnimator.start();
    }
}
